package com.ay.weixin.fdc.boss.user.event;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

import org.lizar.config.Config;
import org.lizar.config.keys.Keys;
import org.lizar.controller.Event;
import org.lizar.controller.intercepter.Before;
import org.lizar.controller.loader.BasicLoader;
import org.lizar.controller.loader.EventLoader;
import org.lizar.ioc.Container;
import org.lizar.json.JObject;
import org.lizar.util.MyMath;
import org.lizar.util.StringHelper;
import org.lizar.util.pager.PageData;

import com.ay.weixin.config.ProjectConfig;
import com.ay.weixin.config.types.UserStatus;
import com.ay.weixin.fdc.admin.customer.domain.Customer;
import com.ay.weixin.fdc.boss.customer.event.CustomerLogin;
import com.ay.weixin.fdc.boss.kanfang.db.IbatisKanFangDao;
import com.ay.weixin.fdc.boss.user.db.FDCAgentDao;
import com.ay.weixin.fdc.boss.user.db.WeiXinInfoDao;
import com.ay.weixin.fdc.boss.user.domain.FDCAgent;
import com.ay.weixin.fdc.boss.user.domain.FDCAgentDetail;
import com.ay.weixin.fdc.boss.user.domain.FDCUserCenter;
import com.ay.weixin.fdc.boss.user.domain.UserBasic;
import com.ay.weixin.fdc.boss.user.domain.UserWeiXinInfo;
import com.ay.weixin.fdc.boss.yongjin.db.IbatisCashFlow;
import com.ay.weixin.fdc.boss.yongjin.domain.CashFlow;
import com.ay.weixin.fdc.front.usercenter.event.manager.Register;
import com.ay.weixin.templatereply.domain.ReplyMsg;
import com.ay.weixin.templatereply.domain.TemplateReply;

public class UserMC extends Event implements Before {
	private FDCAgentDao				user_dao;
	
	private Register				register;
	private static IbatisCashFlow	cash_dao;
	private IbatisKanFangDao		kanfang_dao;
	private FDCUserCenter			user_center;
	private static WeiXinInfoDao	weixin_dao;
	
	@Override
	public void init_property() throws Exception {
		user_dao = Container.get(FDCAgentDao.class);
		register = Container.get(Register.class);
		cash_dao = Container.get(IbatisCashFlow.class);
		kanfang_dao = Container.get(IbatisKanFangDao.class);
		user_center = Container.get(FDCUserCenter.class);
		weixin_dao = Container.get(WeiXinInfoDao.class);
		
	}
	
	@Override
	public void before(BasicLoader bl) throws ServletException, IOException {
		long wxproject_id=bl.queryPathInt(2);
		bl.setAttr("mc_user_enable_salesman", ProjectConfig.getBool(wxproject_id,"fdc_usercenter.enable_salesman"));
		
	}
	
	@Override
	public String default_handling_path() {
		
		return "/mc/fdc/user/*";
	}
	
	@Override
	public void handle(EventLoader el) throws ServletException, IOException {
		
		Customer customer = (Customer) el.getSessionAttr("customer");
		if (customer == null) {
			el.response_to_root(CustomerLogin.login_path());
		} else {
			if (el.queryPathCheck(3, "")) {
				index(el,customer.getWxprojectID());
			} else if (el.queryPathCheck(3, "profile_view")) {
				profile_view(el,customer.getWxprojectID());
			} else if (el.queryPathCheck(3, "soft_delete")) {
				soft_delete(el,customer.getWxprojectID());
			} else if (el.queryPathCheck(3, "restore_delete")) {
				restore_delete(el,customer.getWxprojectID());
			} else if (el.queryPathCheck(3, "delete")) {
				delete(el,customer.getWxprojectID());
			} else if (el.queryPathCheck(3, "ok")) {
				ok(el,customer.getWxprojectID());
			} else if (el.queryPathCheck(3, "verify_failed")) {
				verify_failed(el,customer.getWxprojectID());
			} else if (el.queryPathCheck(3, "reset_password")) {
				reset_password(el);
			} else if (el.queryPathCheck(3, "save_type")) {
				save_type(el,customer.getWxprojectID());
			} else if (el.queryPathCheck(3, "clear_today_recommand_num")) {
				clear_today_recommand_num(el);
			} else if (el.queryPathCheck(3, "reset_black_list")) {
				reset_black_list(el);
			} else if (el.queryPathCheck(3, "detail")) {
				detail(el,customer.getWxprojectID());
			} else if (el.queryPathCheck(3, "view_setting")) {
				view_setting(el,customer.getWxprojectID());
			} else if (el.queryPathCheck(3, "setting")) {
				setting(el,customer.getWxprojectID());
			} else if (el.queryPathCheck(3, "jingjiren")) {
				jingjiren(el,customer.getWxprojectID());
			} else if (el.queryPathCheck(3, "salesman")) {
				salesman(el,customer.getWxprojectID());
			} else if (el.queryPathCheck(3, "save_type_submit")) {
				save_type_submit(el,customer.getWxprojectID());
			}
		}
	}
	
	private void save_type_submit(EventLoader el,long wxproject_id) throws IOException, ServletException{
		int id=el.getInt("id");
		String type=el.getStr("type");
		if(!el.paramExists("id")||!el.paramIsInteger("id")){
			el.setAttr("msg", el.i18("mc.user.id.needed"));
			index(el,wxproject_id);
			return;
		}
		try {
			user_dao.updateType(id,type);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg", el.i18("fdc_user.db.failed"));
			index(el,wxproject_id);
			return;
		}
		
		HttpSession session=user_center.getSession(id);
		if(session!=null){
			FDCAgent user=(FDCAgent)session.getAttribute("user");
			if(user!=null){
				user.setType(type);
			}
		}
		
		change_user_type_update(wxproject_id,type,id);
		
		el.setAttr("msg", el.i18("fd_user.operation.success"));
		index(el,wxproject_id);
	}
	
	
	/**
	 * 
	 *  
	 * 
	 * {{first.DATA}}\n变更内容：{{keyword1.DATA}}\n时间：{{keyword2.DATA}}\n{{remark.DATA}}
	 * 
	 * */
	public static void change_user_type_update(long wxproject_id,String to_type,long user_id){
		UserWeiXinInfo weixin=null;
		try {
			weixin=weixin_dao.getByUserId(user_id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(weixin==null||StringHelper.isNull(weixin.getId())){
			System.out.println("user openid not found");
			return;
		}
		
		String name="经纪人";
		if(to_type.equals("old")){
			name="老带新";
		}else if(to_type.equals("share")){
			name="游客";
		}else if(to_type.equals("salesman")){
			name="销售";
		}
		
		JObject params=new JObject();
		
		
		JObject keyword1=new JObject();
		keyword1.put("value","账户类型："+name);
		keyword1.put("color", ReplyMsg.BLUE);
		params.put("keyword1", keyword1);
		
		JObject remark=new JObject();
		remark.put("value", "亲爱的,您的账户类型已变更,如有疑问,请联络客服.");
		remark.put("color", ReplyMsg.BLUE);
		params.put("remark",remark );
		
		JObject keyword2=new JObject();
		keyword2.put("value",Calendar.getInstance().getTime().toLocaleString());
		keyword2.put("color", ReplyMsg.LIGHT_BLACK);
		params.put("keyword2", keyword2);
		
		
		ReplyMsg msg=new ReplyMsg();
		msg.setMsg_link("");
		msg.setOpenid(weixin.getId());
		msg.setWxproject_id(wxproject_id);
		msg.setTemplate_id(ProjectConfig.getStr(wxproject_id,"fdc_usercenter.account_update_template_msg_id"));
		msg.setTopcolor(ReplyMsg.BLUE);
		msg.setData_params(params);
		TemplateReply.asyReply(msg);
	}
	
	private void jingjiren(EventLoader el,long wxproject_id) throws IOException, ServletException {
		long id = el.getLong("id");
		if (!el.paramExists("id") || !el.paramIsLong("id")) {
			index(el,wxproject_id);
			return;
		}
		FDCAgentDetail user = null;
		String msg = null;
		try {
			user = user_dao.getUserDetailById(id);
		} catch (SQLException e) {
			e.printStackTrace();
			msg = el.i18("fdc_user.db.failed");
			el.setAttr("msg", msg);
			index(el,wxproject_id);
			return;
		}
		
		if (user == null) {
			msg = el.i18("fdc_user_center.user.not.exists");
			el.setAttr("msg", msg);
			index(el,wxproject_id);
			return;
		}
		
		List<UserBasic> ulist = null;
		
		try {
			ulist = user_dao.getMyInviteListFirstPage(user.getInvite_code());
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg", el.i18("db.failure"));
		}
		el.setAttr("user_detail", user);
		el.setAttr("ulist", ulist);
		
		el.template("/WEB-INF/template/boss/fdc/user/jingjiren.vm");
	}
	
	private void detail(EventLoader el,long wxproject_id) throws IOException, ServletException {
		long id = el.getLong("id");
		if (!el.paramExists("id") || !el.paramIsLong("id")) {
			index(el,wxproject_id);
			return;
		}
		FDCAgentDetail user = null;
		String msg = null;
		float closed_commission = 0;
		float not_closed = 0;
		try {
			user = user_dao.getUserDetailById(id);
		} catch (SQLException e) {
			e.printStackTrace();
			msg = el.i18("fdc_user.db.failed");
			el.setAttr("msg", msg);
			index(el,wxproject_id);
			return;
		}
		
		if (user == null) {
			msg = el.i18("fdc_user_center.user.not.exists");
			el.setAttr("msg", msg);
			index(el,wxproject_id);
			return;
		}
		
		try {
			not_closed = cash_dao.countSpecificInStatusFlow(user.getId(), CashFlow.IN_STATUS_IN);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg", el.i18("bank.db.failure"));
		}
		
		try {
			closed_commission = cash_dao.countCashAll(user.getId(), CashFlow.FLOW_OUT);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg", el.i18("bank.db.failure"));
		}
		
		int recommand = 0;
		
		try {
			recommand = kanfang_dao.getRecommandNumber(user.getId());
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg", el.i18("fdc.project.db.failure"));
		}
		
		if (StringHelper.isNotNull(user.getFrom_invite_code())) {
			UserBasic supper_manager = null;
			try {
				supper_manager = user_dao.getByInviteCode(user.getFrom_invite_code());
			} catch (SQLException e) {
				e.printStackTrace();
				el.setAttr("msg", el.i18("fdc.project.db.failure"));
			}
			el.setAttr("supper_manager", supper_manager);
		}
		
		el.setAttr("recommand", recommand);
		el.setAttr("not_closed", not_closed);
		el.setAttr("closed_commission", closed_commission);
		el.setAttr("user_detail", user);
		el.setAttr("user_black_list_limit", ProjectConfig.getStr(wxproject_id,"fdc_kanfang.user_black_list_limit"));
		el.template("/WEB-INF/template/boss/fdc/user/detail.vm");
	}
	
	private void clear_today_recommand_num(EventLoader el) throws IOException, ServletException {
		long id = el.getLong("id");
		JObject result = new JObject();
		if (!el.paramExists("id") || !el.paramIsLong("id")) {
			result.put("err", el.i18("mc.user.id.needed"));
			el.text(result.toString());
			return;
		}
		try {
			user_dao.clear_today_recommand_num(id);
		} catch (SQLException e) {
			e.printStackTrace();
			result.put("err", el.i18("fdc_user.db.failed"));
			el.text(result.toString());
			return;
		}
		
		HttpSession session = user_center.getSession(id);
		if (session != null) {
			FDCAgent user = (FDCAgent) session.getAttribute("user");
			if (user != null) {
				user.setToday_recommand_num(0);
			}
		}
		
		result.put("success", el.i18("fd_user.operation.success"));
		el.text(result.toString());
	}
	
	private void reset_black_list(EventLoader el) throws IOException, ServletException {
		long id = el.getLong("id");
		JObject result = new JObject();
		if (!el.paramExists("id") || !el.paramIsLong("id")) {
			result.put("err", el.i18("mc.user.id.needed"));
			el.text(result.toString());
			return;
		}
		try {
			user_dao.resetBlackList(id);
		} catch (SQLException e) {
			e.printStackTrace();
			result.put("err", el.i18("fdc_user.db.failed"));
			el.text(result.toString());
			return;
			
		}
		
		HttpSession session = user_center.getSession(id);
		if (session != null) {
			FDCAgent user = (FDCAgent) session.getAttribute("user");
			if (user != null) {
				user.setBlack_list_times(0);
			}
		}
		
		result.put("success", el.i18("fd_user.operation.success"));
		el.text(result.toString());
	}
	
	private void save_type(EventLoader el,long wxproject_id) throws IOException, ServletException {
		int id = el.getInt("id");
		JObject result = new JObject();
		String type = el.getStr("type");
		if (!el.paramExists("id") || !el.paramIsInteger("id")) {
			result.put("err", el.i18("mc.user.id.needed"));
			el.text(result.toString());
			return;
		}
		try {
			user_dao.updateType(id, type);
		} catch (SQLException e) {
			e.printStackTrace();
			result.put("err", el.i18("fdc_user.db.failed"));
			el.text(result.toString());
			return;
		}
		
		HttpSession session = user_center.getSession(id);
		if (session != null) {
			FDCAgent user = (FDCAgent) session.getAttribute("user");
			if (user != null) {
				user.setType(type);
			}
		}
		change_user_type_update(wxproject_id,type,id);
		result.put("success", el.i18("fd_user.operation.success"));
		el.text(result.toString());
	}
	
	private void reset_password(EventLoader el) throws IOException, ServletException {
		int id = el.getInt("id");
		JObject result = new JObject();
		String password = el.getStr("password");
		if (!el.paramExists("id") || !el.paramIsInteger("id")) {
			result.put("err", el.i18("mc.user.id.needed"));
			el.text(result.toString());
			return;
		}
		try {
			user_dao.updatePassword(id, MyMath.encryptionWithMD5(password));
		} catch (SQLException e) {
			e.printStackTrace();
			result.put("err", el.i18("fdc_user.db.failed"));
			el.text(result.toString());
			return;
		}
		result.put("success", el.i18("fd_user.operation.success"));
		el.text(result.toString());
	}
	
	private void setting(EventLoader el,long wxproject_id) throws IOException, ServletException {
		boolean register_need_verify_manually = el.getBool("register_need_verify_manually");
		boolean register_need_upload_business_card = el.getBool("register_need_upload_business_card");
		boolean register_need_upload_ID_card = el.getBool("register_need_upload_ID_card");
		boolean register_need_invate_code = el.getBool("register_need_invate_code");
		boolean register_need_old_house = el.getBool("register_need_old_house");
		boolean register_invate_benifit = el.getBool("register_invate_benifit");
		String register_invate_benifit_rules = el.getStr("register_invate_benifit_rules");
		String scope_level = el.getStr("scope_level");
		
		String msg = null;
		try {
			ProjectConfig.update(wxproject_id, "fdc_usercenter.register_need_verify_manually", register_need_verify_manually);
			ProjectConfig.update(wxproject_id, "fdc_usercenter.register_need_upload_business_card", register_need_upload_business_card);
			ProjectConfig.update(wxproject_id, "fdc_usercenter.register_need_upload_ID_card", register_need_upload_ID_card);
			ProjectConfig.update(wxproject_id, "fdc_usercenter.register_need_invate_code", register_need_invate_code);
			ProjectConfig.update(wxproject_id, "fdc_usercenter.register_need_old_house", register_need_old_house);
			ProjectConfig.update(wxproject_id, "fdc_usercenter.register_invate_benifit", register_invate_benifit);
			ProjectConfig.update(wxproject_id, "fdc_usercenter.register_invate_benifit_rules", register_invate_benifit_rules);
			ProjectConfig.update(wxproject_id, "fdc_usercenter.register_confirm_benifit", el.getBool("register_confirm_benifit"));
			ProjectConfig.update(wxproject_id, "fdc_usercenter.register_benifit_for_new_commer", el.getBool("register_benifit_for_new_commer"));
			ProjectConfig.update(wxproject_id, "fdc_usercenter.register_benifit_value", el.getStr("register_benifit_value"));
			ProjectConfig.update(wxproject_id, "fdc_usercenter.register_invate_benifit_value", el.getStr("register_invate_benifit_value"));
			ProjectConfig.update(wxproject_id, "weixin_api_user.scope_level", scope_level);
			ProjectConfig.update(wxproject_id, "fdc_usercenter.enable_salesman", el.getBool("mc_user_enable_salesman"));
		} catch (Exception e) {
			e.printStackTrace();
			msg = el.i18("replysetting.default_msg_reply.failured") + ":" + e.getMessage();
		}
		if (msg == null) {
			msg = el.i18("fdc.user.setting.success");
		}
		el.setAttr("msg", msg);
		
		view_setting(el,wxproject_id);
	}
	
	private void view_setting(EventLoader el,long wxproject_id) throws IOException, ServletException {
		
		el.setAttr("mc_user_enable_salesman", ProjectConfig.getStr(wxproject_id,"fdc_usercenter.enable_salesman"));
		el.setAttr("register_need_verify_manually", ProjectConfig.getStr(wxproject_id,"fdc_usercenter.register_need_verify_manually"));
		el.setAttr("register_need_upload_business_card", ProjectConfig.getStr(wxproject_id,"fdc_usercenter.register_need_upload_business_card"));
		el.setAttr("register_need_upload_ID_card", ProjectConfig.getStr(wxproject_id,"fdc_usercenter.register_need_upload_ID_card"));
		el.setAttr("register_need_invate_code", ProjectConfig.getStr(wxproject_id,"fdc_usercenter.register_need_invate_code"));
		el.setAttr("register_need_old_house", ProjectConfig.getStr(wxproject_id,"fdc_usercenter.register_need_old_house"));
		el.setAttr("scope_level", ProjectConfig.getStr(wxproject_id,"weixin_api_user.scope_level"));
		el.setAttr("register_confirm_benifit", ProjectConfig.getStr(wxproject_id,"fdc_usercenter.register_confirm_benifit"));
		el.setAttr("register_invate_benifit", ProjectConfig.getStr(wxproject_id,"fdc_usercenter.register_invate_benifit"));
		el.setAttr("register_benifit_for_new_commer", ProjectConfig.getStr(wxproject_id,"fdc_usercenter.register_benifit_for_new_commer"));
		el.setAttr("register_benifit_value", ProjectConfig.getStr(wxproject_id,"fdc_usercenter.register_benifit_value"));
		el.setAttr("register_invate_benifit_value", ProjectConfig.getStr(wxproject_id,"fdc_usercenter.register_invate_benifit_value"));
		el.setAttr("register_invate_benifit_rules", ProjectConfig.getStr(wxproject_id,"fdc_usercenter.register_invate_benifit_rules"));
		
		el.setAttr("register_forced_business_card", ProjectConfig.getStr(wxproject_id,"fdc_usercenter.register_forced_business_card"));
		el.setAttr("register_forced_ID_card", ProjectConfig.getStr(wxproject_id,"fdc_usercenter.register_forced_ID_card"));
		
		el.template("/WEB-INF/template/boss/fdc/user/setting.vm");
	}
	
	private void ok(EventLoader el,long wxproject_id) throws IOException, ServletException {
		String msg = ok_ext(el,wxproject_id);
		el.setAttr("msg", msg);
		index(el,wxproject_id);
	}
	
	private void verify_failed(EventLoader el,long wxproject_id) throws IOException, ServletException {
		String msg = verify_failed_ext(el,wxproject_id);
		el.setAttr("msg", msg);
		index(el,wxproject_id);
	}
	
	private String verify_failed_ext(EventLoader el,long wxproject_id) {
		long id = el.getLong("id");
		FDCAgent user = null;
		try {
			user = user_dao.getById(id);
		} catch (SQLException e1) {
			e1.printStackTrace();
			return el.i18("fdc_user.db.failed");
		}
		if (user == null) {
			return el.i18("fdc_user_center.user.not.exists");
		}
		try {
			user.setStatus(UserStatus.apply.name());
			user.setProfile_verify(UserBasic.PROFILE.VERIFY_FAILED.getValue());
			user_dao.updateStatus(user.getId(), UserStatus.apply.name(), UserBasic.PROFILE.VERIFY_FAILED.getValue());
		} catch (SQLException e) {
			e.printStackTrace();
			return el.i18("fdc_user.db.failed");
		}
		HttpSession session = user_center.getSession(id);
		if (session != null) {
			session.setAttribute("user", user);
		}
		
		template_msg_status_update(wxproject_id,UserBasic.PROFILE.VERIFY_FAILED.getDesc(), user.getId());
		
		return el.i18("fd_user.operation.success");
		
	}
	
	/**
	 * 
	 *  
	 * 
	 * {{first.DATA}}\n变更内容：{{keyword1.DATA}}\n时间：{{keyword2.DATA}}\n{{remark.DATA}}
	 * 
	 * */
	public static void template_msg_status_update(long wxproject_id,String status_name, long user_id) {
		UserWeiXinInfo weixin = null;
		try {
			weixin = weixin_dao.getByUserId(user_id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (weixin == null || StringHelper.isNull(weixin.getId())) {
			System.out.println("user openid not found");
			return;
		}
		
		JObject params = new JObject();
		
		JObject keyword1 = new JObject();
		keyword1.put("value", "账户状态：" + status_name);
		keyword1.put("color", ReplyMsg.BLUE);
		params.put("keyword1", keyword1);
		
		JObject remark = new JObject();
		remark.put("value", "亲爱的,您的账户状态已变更,如有疑问,请联络客服.");
		remark.put("color", ReplyMsg.BLUE);
		params.put("remark", remark);
		
		JObject keyword2 = new JObject();
		keyword2.put("value", Calendar.getInstance().getTime().toLocaleString());
		keyword2.put("color", ReplyMsg.LIGHT_BLACK);
		params.put("keyword2", keyword2);
		
		ReplyMsg msg = new ReplyMsg();
		msg.setMsg_link("");
		msg.setOpenid(weixin.getId());
		msg.setWxproject_id(wxproject_id);
		msg.setTemplate_id(ProjectConfig.getStr(wxproject_id,"fdc_usercenter.account_update_template_msg_id"));
		msg.setTopcolor(ReplyMsg.BLUE);
		msg.setData_params(params);
		TemplateReply.asyReply(msg);
	}
	
	private String ok_ext(EventLoader el,long wxproject_id) {
		long id = el.getLong("id");
		FDCAgent user = null;
		try {
			user = user_dao.getById(id);
		} catch (SQLException e1) {
			e1.printStackTrace();
			return el.i18("fdc_user.db.failed");
		}
		if (user == null) {
			return el.i18("fdc_user_center.user.not.exists");
		}
		try {
			user.setStatus(UserStatus.ok.name());
			user.setProfile_verify(UserBasic.PROFILE.VERIFIED.getValue());
			user_dao.updateStatus(user.getId(), UserStatus.ok.name(), UserBasic.PROFILE.VERIFIED.getValue());
		} catch (SQLException e) {
			e.printStackTrace();
			return el.i18("fdc_user.db.failed");
		}
		
		if (StringHelper.isNotNull(user.getFrom_invite_code())) {
			FDCAgent from_user = null;
			try {
				from_user = user_dao.getByInviteCode(user.getFrom_invite_code());
			} catch (SQLException e) {
				e.printStackTrace();
				return el.i18("fdc_user.db.failed");
			}
			register.register_income(el,wxproject_id, user.getFrom_invite_code(), user, from_user);
		}
		HttpSession session = user_center.getSession(id);
		if (session != null) {
			session.setAttribute("user", user);
		}
		template_msg_status_update(wxproject_id,"审核通过", user.getId());
		return el.i18("fd_user.operation.success");
		
	}
	
	private void delete(EventLoader el,long wxproject_id) throws IOException, ServletException {
		String msg = delete_ext(el);
		el.setAttr("msg", msg);
		index(el,wxproject_id);
	}
	
	private String delete_ext(EventLoader el) {
		long id = el.getLong("id");
		FDCAgent user = null;
		try {
			user = user_dao.getById(id);
		} catch (SQLException e1) {
			e1.printStackTrace();
			return el.i18("fdc_user.db.failed");
		}
		if (user == null) {
			return el.i18("fdc_user_center.user.not.exists");
		}
		
		try {
			
			user_dao.delete(id);
		} catch (SQLException e) {
			e.printStackTrace();
			return el.i18("fdc_user.db.failed");
		}
		
		if (StringHelper.isNotNull(user.getBusiness_card_url())) {
			File f = new File(el.getRealPath(user.getBusiness_card_url()));
			f.delete();
		}
		if (StringHelper.isNotNull(user.getID_card_url())) {
			File f = new File(el.getRealPath(user.getID_card_url()));
			f.delete();
		}
		return el.i18("fdc_user.delete.success");
		
	}
	
	private void soft_delete(EventLoader el,long wxproject_id) throws IOException, ServletException {
		String msg = soft_delete_ext(el,wxproject_id);
		el.setAttr("msg", msg);
		index(el,wxproject_id);
	}
	
	private String soft_delete_ext(EventLoader el,long wxproject_id) {
		long id = el.getLong("id");
		FDCAgent user = null;
		try {
			user = user_dao.getById(id);
		} catch (SQLException e1) {
			e1.printStackTrace();
			return el.i18("fdc_user.db.failed");
		}
		if (user == null) {
			return el.i18("fdc_user_center.user.not.exists");
		}
		
		try {
			user.setStatus(UserStatus.deleted.name());
			user.setProfile_verify(UserBasic.PROFILE.VERIFY_FAILED.getValue());
			user_dao.softDelete(user.getId(), UserStatus.deleted.name());
		} catch (SQLException e) {
			e.printStackTrace();
			return el.i18("fdc_user.db.failed");
		}
		template_msg_status_update(wxproject_id,"已删除", user.getId());
		return el.i18("fd_user.delete.success");
		
	}
	
	private void restore_delete(EventLoader el,long wxproject_id) throws IOException, ServletException {
		String msg = restore_delete_ext(el,wxproject_id);
		el.setAttr("msg", msg);
		index(el,wxproject_id);
	}
	
	private String restore_delete_ext(EventLoader el,long wxproject_id) {
		long id = el.getLong("id");
		FDCAgent user = null;
		try {
			user = user_dao.getById(id);
		} catch (SQLException e1) {
			e1.printStackTrace();
			return el.i18("fdc_user.db.failed");
		}
		if (user == null) {
			return el.i18("fdc_user_center.user.not.exists");
		}
		
		try {
			user_dao.updateStatus(user.getId(), UserStatus.ok.name(), user.getProfile_verify());
		} catch (SQLException e) {
			e.printStackTrace();
			return el.i18("fdc_user.db.failed");
		}
		template_msg_status_update(wxproject_id,"恢复正常", user.getId());
		return el.i18("fd_user.restore.success");
		
	}
	
	private void profile_view(EventLoader el,long wxproject_id) throws IOException, ServletException {
		int profile_verify = el.getInt("profile_verify", UserBasic.PROFILE.NOT_VERIFY.getValue());
		String keywords = el.param("keywords", "", "utf-8");
		int page = el.getInt("page", 1);
		int size = Keys.getInt("sys_page_size");
		int total = 0;
		String msg = null;
		try {
			if (StringHelper.isNull(keywords)) {
				total = user_dao.countByProfile(wxproject_id,profile_verify);
			} else {
				total = user_dao.countSearchByProfile(wxproject_id,profile_verify, keywords);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = el.i18("kanfang.yuyue.db.failure");
			el.setAttr("msg", msg);
		}
		
		PageData page_data = new PageData<FDCAgentDetail>(page, total, size, null);
		
		int start = page_data.getStart();
		
		List<FDCAgentDetail> list = null;
		
		try {
			if (StringHelper.isNull(keywords)) {
				list = user_dao.getUserDetailByProfile(wxproject_id,profile_verify, start, size);
			} else {
				System.out.println(keywords + " " + start + " " + size);
				list = user_dao.searchUserDetailByProfile(wxproject_id,profile_verify, keywords, start, size);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			msg = el.i18("kanfang.yuyue.db.failure");
			el.setAttr("msg", msg);
		}
		page_data.setData(list);
		
		el.setAttr("profile_verify", profile_verify);
		el.setAttr("page_data", page_data);
		el.setAttr("keywords", keywords);
		el.setAttr("keywords_encode", URLEncoder.encode(keywords, "utf-8"));
		el.setAttr("profile_values", UserBasic.PROFILE.values());
		el.setAttr("status_list", UserStatus.values());
		el.setAttr("list", list);
		el.template("/WEB-INF/template/boss/fdc/user/profile.vm");
	}
	
	private void index(EventLoader el,long wxproject_id) throws IOException, ServletException {
		String status = el.getStr("status",UserStatus.ok.name());
		String keywords = el.param("keywords", "", "utf-8");
		int page = el.getInt("page", 1);
		int size = Keys.getInt("sys_page_size");
		int total = 0;
		String msg = null;
		try {
			if (StringHelper.isNull(keywords)) {
				total = user_dao.countByStatus(wxproject_id,status);
			} else {
				total = user_dao.countSearchByStatus(wxproject_id,status, keywords);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = el.i18("kanfang.yuyue.db.failure");
			el.setAttr("msg", msg);
		}
		
		PageData page_data = new PageData<FDCAgentDetail>(page, total, size, null);
		
		int start = page_data.getStart();
		
		List<FDCAgentDetail> list = null;
		
		try {
			if (StringHelper.isNull(keywords)) {
				list = user_dao.getUserDetailByStatus(wxproject_id,status, start, size);
			} else {
				System.out.println(keywords + " " + start + " " + size);
				list = user_dao.searchUserDetailByStatus(wxproject_id,status, keywords, start, size);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			msg = el.i18("kanfang.yuyue.db.failure");
			el.setAttr("msg", msg);
		}
		page_data.setData(list);
		
		el.setAttr("page_data", page_data);
		el.setAttr("keywords", keywords);
		el.setAttr("keywords_encode", URLEncoder.encode(keywords, "utf-8"));
		el.setAttr("status", status);
		el.setAttr("profile_values", UserBasic.PROFILE.values());
		el.setAttr("status_list", UserStatus.values());
		el.setAttr("list", list);
		el.template("/WEB-INF/template/boss/fdc/user/index.vm");
	}
	
	private void salesman(EventLoader el,long wxproject_id) throws IOException, ServletException {
		String status = el.getStr("status",UserStatus.ok.name());
		String keywords = el.param("keywords", "", "utf-8");
		int page = el.getInt("page", 1);
		int size = Keys.getInt("sys_page_size");
		int total = 0;
		String msg = null;
		try {
			if (StringHelper.isNull(keywords)) {
				total = user_dao.countSalesmanByStatus(wxproject_id,status);
			} else {
				total = user_dao.countSalesmanSearchByStatus(wxproject_id,status, keywords);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = el.i18("kanfang.yuyue.db.failure");
			el.setAttr("msg", msg);
		}
		
		PageData page_data = new PageData<FDCAgentDetail>(page, total, size, null);
		
		int start = page_data.getStart();
		
		List<FDCAgentDetail> list = null;
		
		try {
			if (StringHelper.isNull(keywords)) {
				list = user_dao.getSalesmanByStatus(wxproject_id,status, start, size);
			} else {
				System.out.println(keywords + " " + start + " " + size);
				list = user_dao.searchSalesmanByStatus(wxproject_id,status, keywords, start, size);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			msg = el.i18("kanfang.yuyue.db.failure");
			el.setAttr("msg", msg);
		}
		page_data.setData(list);
		
		el.setAttr("page_data", page_data);
		el.setAttr("keywords", keywords);
		el.setAttr("keywords_encode", URLEncoder.encode(keywords, "utf-8"));
		el.setAttr("status", status);
		el.setAttr("profile_values", UserBasic.PROFILE.values());
		el.setAttr("status_list",UserStatus.values());
		el.setAttr("list", list);
		el.template("/WEB-INF/template/boss/fdc/user/salesman.vm");
	}
	
}
